import requests
import json
class Haotodo:
    def __init__(self):
        self.url2 = 'http://howtodo.yilan.tv/video/play?id='
    def video_url(self,url):
        """
        找寻视频初始网址
        :param url:
        :return:
        """
        response = requests.get(url)
        urls = json.loads(response.text)
        urls = urls.get('contents')
        url_all = []
        for url in urls:
            url_all.append(self.url2 + url.get('id'))
        return url_all

    def haotudo(self,url):
        """
        对爬取的视频URL进行爬取,并下载视频
        :param url
        :return:
        """
        response = requests.get(url)
        content = json.loads(response.text)
        title = content.get('name')
        video_all = content.get('bitrates')
        for video in video_all:
            video = video.get('uri')
            response = requests.get(video)
        with open(u'F:\\8Python数据文件\\1lianxi\\task_0715\\shipin\\video'+'/'+title+'.mp4','wb') as file:
            file.write(response.content)
if __name__ == '__main__':
    url = 'http://howtodo.yilan.tv/video/v2/channelhead?app=howto_a&gcid=a4e5a6e92481253b968a819b82a2de36&channel_id=104'
    a = Haotodo()
    for i in a.video_url(url):
        a.haotudo(i)
